﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeFile="Donations.aspx.cs" Inherits="DonationTracker.Donations" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
    <script type="text/javascript" >
        function text_change(t) {
            var val = t.value;
            var l = val.length;
            if (val == "") { l = 0; }
            var btn = document.getElementById('MainContent_GridView1_btnSave');
            if (l > 0) {
                btn.disabled = false;
            } else {
                btn.disabled = true;
            }
        }

        window.onload = function () {
            var d = document.getElementById('grid');
            var g = document.getElementById('MainContent_GridView1');
            d.style.width = g.offsetWidth + "px";
            g.align = "center";
        }
    </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <h2>Donations Page</h2>
    <br /><br />
    <asp:SqlDataSource ID="getCharities" runat="server" 
        ConnectionString="<%$ ConnectionStrings:DonationConn %>" 
        SelectCommand="SELECT [charityID], [name] FROM [charity] ORDER BY [order]"></asp:SqlDataSource>
    <asp:SqlDataSource ID="getDonors" runat="server" 
                        ConnectionString="<%$ ConnectionStrings:DonationConn %>" 
                        
        SelectCommand="select firstName + ' ' + lastName as name, donorID from donor">
    </asp:SqlDataSource>
    <asp:SqlDataSource ID="donationTracker_dataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:DonationConn %>" 
        SelectCommand="SELECT [donationNumber], firstName + ' ' + lastName as donor, donation.description, charity.name as charity, [dateEntered], [UserName] AS enteredBy FROM [donation] join [donor] on donation.donorID = donor.donorID join charity on donation.charityID = charity.charityID join [aspnet_Users] on donation.enteredBy = aspnet_Users.UserID" 
        DeleteCommand="DELETE FROM [donation] WHERE [donationNumber] = @original_donationNumber" 
        InsertCommand="INSERT INTO [donation] ([donorID], [description], [assigned], [charityID], [dateEntered], [enteredBy]) VALUES (@donorID, @description, 'False', @charityID, GETDATE(), @enteredBy)" 
        OldValuesParameterFormatString="original_{0}" 
        
        
        UpdateCommand="UPDATE [donation] SET [donorID] = @donorID, [description] = @description, [charityID] = @charityID WHERE [donationNumber] = @original_donationNumber">
        <DeleteParameters>
            <asp:Parameter Name="original_donationNumber" Type="Int32" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="donorID" Type="Int32" />
            <asp:Parameter Name="description" Type="String" />
            <asp:Parameter Name="charityID" />
            <asp:Parameter Name="enteredBy" Type="Object" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="donorID" Type="Int32" />
            <asp:Parameter Name="description" Type="String" />
            <asp:Parameter Name="charityID" />
            <asp:Parameter Name="original_donationNumber" Type="Int32" />
        </UpdateParameters>
    </asp:SqlDataSource>
    <div id="grid" style="width: 800px; margin: 0px auto;" >
        <asp:GridView ID="GridView1" runat="server" 
            style="max-height: 400px; width: 800px; margin: 0px auto;" AllowPaging="True" 
            AutoGenerateColumns="False" DataSourceID="donationTracker_dataSource" 
            ShowHeaderWhenEmpty="True" DataKeyNames="donationNumber" 
            onrowcommand="GridView1_RowCommand" onrowupdating="GridView1_RowUpdating" >
            <Columns>
                <asp:TemplateField HeaderText="donationNumber" InsertVisible="False" 
                    SortExpression="donationNumber">
                    <FooterTemplate>
                        <asp:Button ID="btnSave" runat="server" CommandName="Insert" Enabled="False" 
                            Text="Save" />
                        <asp:Button ID="btnCancel" runat="server" CausesValidation="False" 
                            Text="Cancel" onclick="btnCancel_Click" />
                    </FooterTemplate>
                    <EditItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Eval("donationNumber") %>'></asp:Label>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label4" runat="server" Text='<%# Bind("donationNumber") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="donor" SortExpression="donor">
                    <FooterTemplate>
                        <asp:DropDownList ID="ddlDonor" runat="server" DataSourceID="getDonors" 
                            DataTextField="name" DataValueField="donorID">
                        </asp:DropDownList>
                    </FooterTemplate>
                    <EditItemTemplate>
                        <asp:DropDownList ID="ddlEditDonor" runat="server" DataSourceID="getDonors" 
                            DataTextField="name" DataValueField="donorID">
                        </asp:DropDownList>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("donor") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="description" SortExpression="description">
                    <FooterTemplate>
                        <asp:TextBox ID="txtDesc" runat="server" Columns="35" MaxLength="150" onkeyup="text_change(this)" ></asp:TextBox>
                        <asp:RequiredFieldValidator ID="rfvDesc" runat="server" 
                            ControlToValidate="txtDesc" Display="Dynamic" 
                            ErrorMessage="Description cannot be left blank." ForeColor="Red"></asp:RequiredFieldValidator>
                    </FooterTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="txtEditDesc" runat="server" Text='<%# Bind("description") %>' 
                            Columns="35" MaxLength="150"></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("description") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="charity" SortExpression="charity">
                    <FooterTemplate>
                        <asp:DropDownList ID="ddlCharity" runat="server" DataSourceID="getCharities" 
                            DataTextField="name" DataValueField="charityID">
                        </asp:DropDownList>
                        <asp:RequiredFieldValidator ID="rfvCharity" runat="server" 
                            ControlToValidate="ddlCharity" Display="Dynamic" 
                            ErrorMessage="Please select a charity from the list." ForeColor="Red" 
                            InitialValue="1012" SetFocusOnError="True"></asp:RequiredFieldValidator>
                    </FooterTemplate>
                    <EditItemTemplate>
                        <asp:DropDownList ID="ddlEditCharity" runat="server" 
                            DataSourceID="getCharities" DataTextField="name" DataValueField="charityID">
                        </asp:DropDownList>
                        <asp:RequiredFieldValidator ID="rfvEditCharity" runat="server" 
                            ErrorMessage="Please select a charity from the list." 
                            ControlToValidate="ddlEditCharity" Display="Dynamic" ForeColor="Red" 
                            InitialValue="1012" SetFocusOnError="True"></asp:RequiredFieldValidator>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label3" runat="server" Text='<%# Bind("charity") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="dateEntered" DataFormatString="{0:d}" 
                    HeaderText="dateEntered" ReadOnly="True" SortExpression="dateEntered" />
                <asp:BoundField DataField="enteredBy" HeaderText="enteredBy" ReadOnly="True" 
                    SortExpression="enteredBy" />
            </Columns>
        </asp:GridView>
        <asp:Button ID="btnAddDonation" runat="server" Text="Add Donation" 
            onclick="btnAddDonation_Click" />
    </div>
</asp:Content>
